﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace T2task1
{
    class Program
    {
        static void Main(string[] args)
        {
            //链接对象
            string connSto = "server=.;database=Game;Integrated Security=true";
            //创建连接对象
            SqlConnection conn = new SqlConnection(connSto);
            //打开数据库连接
            conn.Open();
            //检测数据库连接是否打开
            if (conn.State==System.Data.ConnectionState.Open)
            {
                Console.WriteLine("连接成功！！！");
                //查询全部游戏信息
                //创建插入的sql语句
                string sql = "select NO,Name,price,stock,price*stock Total from Info";
                //创建Command 对象（命令对象）
                SqlCommand cmd = new SqlCommand(sql, conn);
                //执行SQL语句
                SqlDataReader reader = cmd.ExecuteReader();
                Console.WriteLine("序号\t游戏名称\t游戏介绍\t库存数量\t小计");
                Console.WriteLine("--------------------------------------------------");
                while (reader.Read())
                {
                    Console.WriteLine("{0}\t{1}\t{2:C}元\t\t{3}\t{4:C}元",
                    reader["NO"],reader["Name"],reader["price"],reader["stock"],reader["Total"]
                    );

                }
                reader.Close();
                Console.WriteLine("--------------------------------------------------");
                string totalSql = "select sum(Price*Stock) from Info";
                //创建Command对象（命令对象）
                cmd = new SqlCommand(totalSql,conn);
                string sum = string.Format("{0}", cmd.ExecuteScalar());
                Console.WriteLine("游戏总金额：{0}元",sum);
            }
           
            else
            {
                Console.WriteLine("连接失败！！！");
            }
            //关闭数据库连接
            conn.Close();
        }
    }
}
